package com.techsoul.module.pms.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.techsoul.common.core.pojo.vo.UserVo;
import com.techsoul.common.core.util.ServletUtils;
import com.techsoul.common.core.exception.ParamException;
import com.techsoul.common.core.util.bean.BeanUtils;
import com.techsoul.common.core.util.collection.ListUtils;
import com.techsoul.common.core.util.id.IdWorker;
import com.techsoul.common.core.util.lang.DateUtils;
import com.techsoul.common.security.service.TokenService;
import com.techsoul.module.pms.mapper.*;
import com.techsoul.module.pms.pojo.entity.TbWmsLocation;
import com.techsoul.module.pms.pojo.entity.TbWmsMaterialInLocation;
import com.techsoul.module.pms.pojo.entity.TbWmsMaterialInventoryLending;
import com.techsoul.module.pms.pojo.param.TbWmsLocationSearchParam;
import com.techsoul.module.pms.pojo.param.TbWmsMaterialInLocationSaveParam;
import com.techsoul.module.pms.pojo.param.TbWmsMaterialInLocationSearchParam;
import com.techsoul.module.pms.pojo.param.TbWmsMaterialInventoryLendingSaveParam;
import com.techsoul.module.pms.pojo.vo.TbWmsMaterialInLocationApiVo;
import com.techsoul.module.pms.pojo.vo.TbWmsMaterialInLocationVo;
import com.techsoul.module.pms.pojo.vo.TbWmsMaterialInventoryLendingVo;
import com.techsoul.module.pms.service.ITbWmsMaterialInLocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 货区货物清单 Service实现
 *
 * @author liuen
 * @date 2022-01-25
 */
@Service
@Transactional(readOnly = true)
public class TbWmsMaterialInLocationServiceImpl implements ITbWmsMaterialInLocationService {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private TbWmsMaterialInLocationMapper mapper;
    @Autowired
    private TbWmsMaterialInventoryLendingMapper tbWmsMaterialInventoryLendingMapper;
    @Autowired
    private PmsDeviceInfoMapper pmsDeviceInfoMapper;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private TbUserWarehouseManagerMapMapper tbUserWarehouseManagerMapMapper;

    @Autowired
    private TbWmsLocationMapper tbWmsLocationMapper;

    /**
     * 新增
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public String insert(TbWmsMaterialInLocationSaveParam params) {
        UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        TbWmsMaterialInLocation entity = new TbWmsMaterialInLocation();
        BeanUtils.copyProperties(params, entity);
        entity.setId(idWorker.nextId() + "").preInsert(sysUser.getId());
        this.mapper.insert(entity);
        return entity.getId();
    }
    @Override
    public double countBySkuCode(String skuCode){
        return this.mapper.countBySkuCode(skuCode);
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public Integer delete(String id) {
        return this.mapper.deleteById(id);
    }

    /**
     * 批量删除
     * @param idList
     */
    @Transactional(readOnly = false)
    @Override
    public Integer batchDelete(List<String> idList) {
        if (ListUtils.isNotEmpty(idList)) {
            return this.mapper.deleteBatchIds(idList);
        } else {
            return 0;
        }
    }

    /**
     * 修改
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public Integer update(TbWmsMaterialInLocationSaveParam params) {
        TbWmsMaterialInLocation entityFromDb = this.mapper.selectById(params.getId());
        if (entityFromDb == null) {
            throw new ParamException("id=" + params.getId());
        }
        UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        BeanUtils.copyProperties(params, entityFromDb);
        entityFromDb.preUpdate(sysUser.getId());
        return this.mapper.updateById(entityFromDb);
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public TbWmsMaterialInLocationVo findById(String id) {
        return this.mapper.findById(id);
    }

    /**
     * 列表
     * @param params
     * @return
     */
    @Override
    public List<TbWmsMaterialInLocationVo> findAll(TbWmsMaterialInLocationSearchParam params) {
        UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        List<String> warehouseNos = new ArrayList<String>();
        warehouseNos = this.tbUserWarehouseManagerMapMapper.findAllWarehouseIds(sysUser.getId());
        params.setWarehouseNoList(warehouseNos);
        return this.mapper.findAll(params);
    }

    /**
     * 列表
     * @param params
     * @return
     */
    @Override
    public List<TbWmsMaterialInLocationApiVo> queryDataInfoList(TbWmsMaterialInLocationApiVo params) {
        TbWmsMaterialInLocationSearchParam tbWmsMaterialInLocationSearchParam
                = new TbWmsMaterialInLocationSearchParam();
        IPage<TbWmsMaterialInLocationVo> tbWmsMaterialInLocationVos = null;
        if(params.getStartTime()!=null){
            //    String start = DateUtils.formatDate(params.getStartTime(),"yyyy-MM-dd HH:mm:ss");
            tbWmsMaterialInLocationSearchParam.setCreateTimeBegin(params.getStartTime());
        }
        if(params.getEndTime()!=null){
            //   String end = DateUtils.formatDate(params.getEndTime(),"yyyy-MM-dd HH:mm:ss");
            tbWmsMaterialInLocationSearchParam.setCreateTimeEnd(params.getEndTime());
        }
        if(params.getCellCode()!=null){
            tbWmsMaterialInLocationSearchParam.setCellCode(params.getCellCode());
        }
        if(params.getDeviceCode()!=null){
            tbWmsMaterialInLocationSearchParam.setDeviceCode(params.getDeviceCode());
        }
        if(params.getCellID()!=null){
            //  PmsDeviceInfoVo deviceInfoVo = this.pmsDeviceInfoMapper.findByDeviceCodeAndCellId(params.getDeviceCode(),params.getCellID());
            tbWmsMaterialInLocationSearchParam.setCellNo(params.getCellID());
        }
        if(params.getCellName()!=null){
            tbWmsMaterialInLocationSearchParam.setCellName(params.getCellName());
        }
        if(params.getCorpCode()!=null){
            tbWmsMaterialInLocationSearchParam.setCustomerNo(params.getCorpCode());
        }
        if(params.getPageNum()!=null&&params.getPageSize()!=null){
            tbWmsMaterialInLocationVos=this.mapper.findAll(new Page<>(params.getPageNum(), params.getPageSize()), tbWmsMaterialInLocationSearchParam);
        }else{
            tbWmsMaterialInLocationVos=this.mapper.findAll(new Page<>(1,100), tbWmsMaterialInLocationSearchParam);
        }

        List<TbWmsMaterialInLocationApiVo> apiVos= new ArrayList<TbWmsMaterialInLocationApiVo>();
        List<TbWmsMaterialInLocationVo> tbWmsMaterialInLocationVorts= new ArrayList<TbWmsMaterialInLocationVo>();
        tbWmsMaterialInLocationVorts = tbWmsMaterialInLocationVos.getRecords();
        if(tbWmsMaterialInLocationVorts!=null&&tbWmsMaterialInLocationVorts.size()>0){
            for(int i=0;i<tbWmsMaterialInLocationVorts.size();i++){
                TbWmsMaterialInLocationVo tbWmsMaterialInLocationVo =
                        (TbWmsMaterialInLocationVo)tbWmsMaterialInLocationVorts.get(i);
                TbWmsMaterialInLocationApiVo tbWmsMaterialInLocationApiVo = new TbWmsMaterialInLocationApiVo();
                tbWmsMaterialInLocationApiVo.setId(tbWmsMaterialInLocationVo.getId());

                tbWmsMaterialInLocationApiVo.setEnabled(true);
                tbWmsMaterialInLocationApiVo.setCellCode(tbWmsMaterialInLocationVo.getCellCode());
                tbWmsMaterialInLocationApiVo.setCellID(tbWmsMaterialInLocationVo.getCellNo());
                tbWmsMaterialInLocationApiVo.setCellName(tbWmsMaterialInLocationVo.getCellName());
                tbWmsMaterialInLocationApiVo.setCode(tbWmsMaterialInLocationVo.getCaseNo());
                tbWmsMaterialInLocationApiVo.setCorpCode(tbWmsMaterialInLocationVo.getCustomerNo());
                tbWmsMaterialInLocationApiVo.setCorpName("");
                tbWmsMaterialInLocationApiVo.setCreateBy(tbWmsMaterialInLocationVo.getCreateBy());
                tbWmsMaterialInLocationApiVo.setCreateTime(new Date());
                tbWmsMaterialInLocationApiVo.setCreatorId(tbWmsMaterialInLocationVo.getCreateBy());
                if(tbWmsMaterialInLocationVo.getLocationNo().contains("CB")){
                    tbWmsMaterialInLocationApiVo.setDeviceCode("11111111");
                }else  if(tbWmsMaterialInLocationVo.getLocationNo().contains("CC")){
                    tbWmsMaterialInLocationApiVo.setDeviceCode("22222222");
                }else{
                    tbWmsMaterialInLocationApiVo.setDeviceCode("11111111");
                }

                tbWmsMaterialInLocationApiVo.setEquipmentTypeName("类型名称");
                tbWmsMaterialInLocationApiVo.setEpc(tbWmsMaterialInLocationVo.getRfid());
                tbWmsMaterialInLocationApiVo.setLicenceNumber(tbWmsMaterialInLocationVo.getMaterialNo());
                tbWmsMaterialInLocationApiVo.setModifierId(tbWmsMaterialInLocationVo.getUpdateBy());
                tbWmsMaterialInLocationApiVo.setModifyTime(tbWmsMaterialInLocationVo.getUpdateTime());
                tbWmsMaterialInLocationApiVo.setName(tbWmsMaterialInLocationVo.getCaseName());
                tbWmsMaterialInLocationApiVo.setNowState(1);
                tbWmsMaterialInLocationApiVo.setStateName("在库");
                tbWmsMaterialInLocationApiVo.setReceptionTime(tbWmsMaterialInLocationVo.getCreateTime());
                tbWmsMaterialInLocationApiVo.setSpecifications("");
                tbWmsMaterialInLocationApiVo.setState(1);
                tbWmsMaterialInLocationApiVo.setUserCode(null);
                tbWmsMaterialInLocationApiVo.setUserID(null);
                tbWmsMaterialInLocationApiVo.setUsername(null);
                tbWmsMaterialInLocationApiVo.setBrand(tbWmsMaterialInLocationVo.getMaterialName());

                apiVos.add(tbWmsMaterialInLocationApiVo);
            }
        }
        return apiVos;
    }
    /**
     * 列表-分页
     * @param params
     * @param current
     * @param pageSize
     * @return
     */
    @Override
    public IPage<TbWmsMaterialInLocationVo> findAll(TbWmsMaterialInLocationSearchParam params, Integer current, Integer pageSize) {
        UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        List<String> warehouseNos = new ArrayList<String>();
        warehouseNos = this.tbUserWarehouseManagerMapMapper.findAllWarehouseIds(sysUser.getId());
        params.setWarehouseNoList(warehouseNos);
        return this.mapper.findAll(new Page<>(current, pageSize), params);
    }

    @Override
    @Transactional(readOnly = false)
    public Integer inventoryLending(TbWmsMaterialInventoryLendingSaveParam params) {
        UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        TbWmsMaterialInventoryLending tbWmsMaterialInventoryLending = new TbWmsMaterialInventoryLending();
        BeanUtils.copyProperties(params, tbWmsMaterialInventoryLending);
        for (String id : params.getIds()){
            TbWmsMaterialInLocation entityFromDb = this.mapper.selectById(id);
            /*TbWmsLocationSearchParam tlsp = new TbWmsLocationSearchParam();
            tlsp.setLocationName(entityFromDb.getLocationNo());
            tlsp.setLocationNo(entityFromDb.getLocationNo());
            tlsp.setWarehouseCode(entityFromDb.getWarehouseCode());
            List<TbWmsLocation> list =  tbWmsLocationMapper.findAllEntity2(tlsp);
            list.get(0).setStatus("empty");
            list.get(0).preUpdate(sysUser.getId());
            tbWmsLocationMapper.updateById(list.get(0));*/

            entityFromDb.setStatus(params.getType());
            entityFromDb.preUpdate(sysUser.getId());
            //entityFromDb.setLocationNo("");
            this.mapper.updateById(entityFromDb);

            tbWmsMaterialInventoryLending.setMaterialId(entityFromDb.getId());
            tbWmsMaterialInventoryLending.setId(idWorker.nextId() + "").preInsert(sysUser.getId());
            tbWmsMaterialInventoryLending.setCreateDate(tbWmsMaterialInventoryLending.getCreateTime());
            tbWmsMaterialInventoryLending.setUpdateDate(tbWmsMaterialInventoryLending.getUpdateTime());
            tbWmsMaterialInventoryLending.setStatus("create");
            tbWmsMaterialInventoryLendingMapper.insert(tbWmsMaterialInventoryLending);
        }

        return 1;
    }

    @Override
    @Transactional(readOnly = false)
    public Integer returnCar(TbWmsMaterialInventoryLendingSaveParam params) {
        UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        TbWmsMaterialInLocation entityFromDb = this.mapper.selectById(params.getMaterialId());
        TbWmsMaterialInventoryLendingVo tmil = tbWmsMaterialInventoryLendingMapper.selectByMaterialId(entityFromDb.getId());
        TbWmsMaterialInventoryLending tbWmsMaterialInventoryLending = new TbWmsMaterialInventoryLending();

        TbWmsLocationSearchParam tbWmsLocationSearchParam = new TbWmsLocationSearchParam();
        tbWmsLocationSearchParam.setWarehouseCode(params.getStageNo());
        tbWmsLocationSearchParam.setArea(params.getAreaNo());
        tbWmsLocationSearchParam.setLocationName(params.getLocationNo());
        tbWmsLocationSearchParam.setStatus("full");
        //TbWmsLocation tbWmsLocation = tbWmsLocationMapper.selectById(params.getLocationNo());
        List<TbWmsLocation> tbWmsLocationList = tbWmsLocationMapper.findAllEntity2(tbWmsLocationSearchParam);

        tbWmsLocationList.get(0).setStatus("full");
        tbWmsLocationList.get(0).preUpdate(sysUser.getId());
        tbWmsLocationMapper.updateById(tbWmsLocationList.get(0));


        entityFromDb.setStatus(params.getType());
        entityFromDb.setWarehouseCode(params.getStageNo());
        entityFromDb.setArea(params.getAreaNo());
        entityFromDb.setLocationNo(tbWmsLocationList.get(0).getLocationName());
        entityFromDb.preUpdate(sysUser.getId());
        mapper.updateById(entityFromDb);

        tmil.setReturnDate(params.getReturnDate());
        tmil.setStatus("end");
        BeanUtils.copyProperties(tmil, tbWmsMaterialInventoryLending);
        tbWmsMaterialInventoryLending.preUpdate(sysUser.getId());
        tbWmsMaterialInventoryLendingMapper.updateById(tbWmsMaterialInventoryLending);
        return 1;
    }

}
